<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Order_mdl extends CI_Model {

    const TABLE = 'order';

    public $status = array(
        0 => '未付款',
        1 => '已付款',
        2 => '待发货',
        3 => '待收货',
        4 => '已完成',
        5 => '订单关闭',
        6 => '订单无效',
        7 => '退货/售后'
    );

    public function select(){
        return $this->db->get(self::TABLE)->result_array();
    }

    public function get_one(){
        $res = $this->db->get(self::TABLE)->row_array();
        $class_status = $this->status; 
       	$res['status_name'] = $class_status[$res['status']];
       	
       	//判断发货状态
       	if($res['status'] <= 2){
       		$res['delivery'] = '未发货';
       	}else{
       		$res['delivery'] = date('Y-m-d H:i:s',$res['delivery']);
       	}
       	
       	//判断操作按钮
       	if($res['status'] >= 1 && $res['status'] < 5){
       		$res['action'] = array(
       				'status' => $res['status']+1,
       				'status_name' => $class_status[($res['status']+1)]
       		);
       	}
       	
       	//支付单号
       	if(empty($res['payment'])){
       		$res['payment'] = '未付款';
       	}
       	
       	return $res;
    }

    //统计
    public function get_count(){
        return $this->db->count_all_results(self::TABLE);
    }

    //添加
    public function create($data){
        $data['end_time'] = $data['create_time'] = time();
        return $this->db->insert(self::TABLE,$data);
    }
    //修改
    public function update($data){
        return $this->db->update(self::TABLE,$data);
    }
    //删除
    public function delete(){
        $data = array(
            'status' => 2
        );
        return $this->update($data);
    }

    //订单数据组装
    public function restr($list){
        $this->load->model('goods_mdl');
        $this->load->model('customer_mdl');

        foreach ($list as $k => $v){
            //获取商品信息
            if(!empty($v['goods_id'])){
                $this->db->where('id',$v['goods_id']);
                $this->db->select('id,name,num,sales,price,sprice,status');
                $res_goods = $this->goods_mdl->get_one();
                $list[$k]['goods'] = $res_goods;
            }

            //获取用户信息
            if(!empty($v['u_id'])){
                $this->db->where('id',$v['u_id']);
                $this->db->select('id,c_user,nickname');
                $res_cust = $this->customer_mdl->get_one();
                $list[$k]['customer'] = $res_cust;
            }

            //订单状态
            if(isset($v['status'])){
                $arr = $this->status;
                $list[$k]['status_name'] = $arr[$v['status']];
            }
        }

        return $list;
    }

}

/* End of file Order_mdl.php */
/* Location: ./application/models/Order_mdl.php */